-- RiddleM
-- created by panyl
-- 谜语模块

module("RiddleM", package.seeall);

local riddles = {};

-- 载入配置表
local _init = false;
local loadCsv = function()
    if _init then return end
    _init = true;

    riddles = {};

    local riddle_CSV = get_riddle_CSV();
    for i = 1, #riddle_CSV do
        local row = riddle_CSV[i];
        local class = row.classify;
        local riddleId = row.id;

        riddles[class] = riddles[class] or {};
        riddles[class][riddleId] = parseRiddle(row);
    end
end

-- 模块初始化操作
function init()
    loadCsv();

    EventMgr.removeAll("RiddleM");
end

function parseRiddle(row)
    -- 解析选项
    local options = string.explode(row["option"], "#");

    row["option"] = options;

    return row;
end

function query(class, riddleId)
    local m = riddles[class];

    if not m or not riddleId then
        return m;
    end

    return m[riddleId];
end

-- 返回是否正确答案，答对返回true，答错返回false
function isGuessRight(class, riddleId, option)
    local riddleInfo = query(class, riddleId);
    if not riddleInfo then
        return false;
    end

    if riddleInfo["answer"] == option then
        return true;
    else
        return false;
    end
end

-- 猜谜，答对返回true，答错返回false
function guessRiddle(class, riddleId, option, para)
    cclog("guessRiddle, class:"..class..", riddleId:"..riddleId..", option:"..option);

    local riddleInfo = query(class, riddleId);
    if not riddleInfo then
        return false;
    end

    -- 答对就奖励，答错就惩罚
    if riddleInfo["answer"] == option then
        doBonus(class, riddleId);

        return true;
    else
        doPunish(class, riddleId, para);

        return false;
    end
end

-- 获得奖励列表
function getBonus(class, riddleId)
    local riddleInfo = query(class, riddleId);
    local bonus = riddleInfo["bonus"];

    local formula;
    if type(bonus) == "string" and string.startWith(bonus, "formula_") then
        formula = string.sub(bonus, string.len("formula_") + 1);
        bonus = FormulaM.invoke(formula, ME.user, class, riddleId);
    end

    if type(bonus) ~= "table" then
        return {};
    end

    return bonus;
end

-- 奖励
function doBonus(class, riddleId)
    local bonus = getBonus(class, riddleId);

    -- 多个奖励
    if type(bonus[1]) == "table" then
        for _, arr in pairs(bonus) do
            BonusM.doBonus(arr, "riddle_" .. class);
        end
    else
        BonusM.doBonus(bonus, "riddle_" .. class);
    end

    return true;
end

-- 惩罚
function doPunish(class, riddleId, para)
    local riddleInfo = query(class, riddleId);
    local punishInfo = riddleInfo["punish"];

    if not punishInfo then
        return false;
    end

    local formula;
    if type(punishInfo) == "string" and string.startWith(punishInfo, "formula_") then
        formula = string.sub(punishInfo, string.len("formula_") + 1);
        punishInfo = FormulaM.invoke(formula, ME.user, class, riddleId);
    end

    -- 需要先排序一遍（为了和服务器一致）
    local keys = table.keys(punishInfo);
    table.sort(keys);

    for _, key in pairs(keys) do
        local value = punishInfo[key];

        -- 伤害
        if key == "damage" then
            -- 直接扣血
            local damage = CombatM.finalDamageFix(ME.user, ME.user, value, 0);
            ME.user:receiveDamage(damage, true);

            -- 抛出事件
            local para = table.add(para, {["damage"] = value, });
            EventMgr.fire(event.RIDDLE_PUNISH_DAMAGE, para);
        end
    end

    return true;
end
